<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title" style="margin-bottom: 30px">
         易行科技系统
      </div>
      <el-form
        :model="user"
        :rules="rules"
        ref="loginForm"
        label-width="0px"
      >
        <el-form-item prop="name">
          <el-input
            v-model="user.name"
            placeholder="请输入手机号码"
            suffix-icon="fa fa-mobile"
            @keyup.enter.native="submitForm('loginForm')"
          >
            <template slot="prepend">
              手机号
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="user.password"
            suffix-icon="fa fa-lock"
            @keyup.enter.native="submitForm('loginForm')"
          >
            <template slot="prepend">
              密码
            </template>

          </el-input>
        </el-form-item>
        <el-form-item prop="rememberMe">
          <el-checkbox v-model="user.rememberMe">
            记住我
          </el-checkbox>
        </el-form-item>
        <div class="login-btn">
          <el-button
            type="primary"
            @click="submitForm('loginForm')"
          >
            登录
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState } from "vuex";
import installRoles from "@/common/roles";
export default {
  data: function() {
    return {
      user: {
        name: "admin",
        password: "admin",
        uuid: "",
        rememberMe: true
      },
      sendBtn: {
        time: 0,
        timer: undefined
      },
      rules: {
        name: [{ required: true, message: "请输入手机号码", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  computed: {
    ...mapState({
      codebooks: state => state.codebooks.codebooks,
      token: state => state.token.token || "",
      cachedTime: state => state.token.time || 0
    })
  },
  mounted() {
    if (this.token) {
      this.$api.user.check(
        () => {
          this.$router.push("/farmer");
        },
        () => {}
      );
    }
    this.sendBtn.time = this.cachedTime;
    if (this.sendBtn.time > 0) {
      this.cutDown();
    }
  },
  methods: {
    ...mapMutations([
      "login",
      "logout",
      "save",
      "remove",
      "updateAvatar",
      "cacheCodebooks",
      "captchaCutDown"
    ]),
    cutDown() {
      this.sendBtn.timer = setInterval(() => {
        if (this.sendBtn.time == 1 && this.sendBtn.timer) {
          clearInterval(this.sendBtn.timer);
        }
        this.sendBtn.time--;
        this.captchaCutDown(this.sendBtn.time);
      }, 1000);
    },
    sendSms() {
      if (this.user.name) {
        this.$api.user.captcha(this.user.name, data => {
          this.user.uuid = data.uuid;
          this.sendBtn.time = 60;
          this.cutDown();
        });
      } else {
        this.$message.error("请输入手机号码");
        return;
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.user.name == 'admin' && this.user.password == 'admin') {

            this.$router.push({ path: "/farmer" });
          }
          // this.$api.user.login(this.user, data => {
          //   let hasRole = false;
          //   if (!data.roles.forEach(v =>{
          //     if(v === installRoles.roles.companyAdmin || v === installRoles.roles.superAdmin) {
          //       hasRole = true;
          //     }
          //   })) ;
          //   if (!hasRole) {
          //     this.$message.error("你暂无权限操作后台系统!");
          //     return;
          //   }
          //   let user = data.user;
          //   this.login(user.token);
          //   user.roles = data.roles;
          //   user.permissions = data.permissions;
          //   this.save(user);
          //   this.$router.push({ path: "/farmer" });
          // });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #f5f7f9;
  -moz-background-size: 100% 100%;
  background-image: url(../assets/login-bg.png);
  background-size: 100% 100%;
}

.append-img {
  height: 32px;
  cursor: pointer;
  margin-left: -20px;
  margin-right: -21px;
  margin-bottom: -5px;
}

.ms-title {
  font-size: 30px;
  font-weight: 900;
  width: 100%;
  text-align: center;
  color: #666666;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}

.ms-login {
  position: absolute;
  left: 80%;
  top: 45%;
  width: 300px;
  height: 350px;
  margin: -150px 0 0 -190px;
  padding: 10px 40px;
  border-radius: 5px;
  background: #fff;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
}
</style>